package com.cloudm.technician.ui.modules.cloudm_box.channel_list

import com.cloudm.technician.data.model.ChannelInfo
import com.cloudm.technician.data.network.CloudmBoxApi
import com.cloudm.technician.ui.base.RxPresenter

/**
 * 作者: wesley
 * 时间: 2017/12/23
 * 描述: 业务逻辑
 */
class ChannelListPresenter(private val view: ChannelListContract.View,
                           private val cloudmBoxApi: CloudmBoxApi) : RxPresenter(),
        ChannelListContract.Presenter {

    override fun getChannelInfo(page: Int, size: Int, deviceId: Int) {
        request(cloudmBoxApi.getDeviceChannel(page, size, deviceId), view,
                onSuccess = { list, loadMoreInfo ->
                    when (page) {
                        1 -> {
                            if (list == null || list.size == 0) {
                                view.openTransferActivity(null)
                                view.finishSelf()
                            } else {
                                view.loadSuccess(list, loadMoreInfo)
                            }
                        }
                        else -> view.loadSuccess(list, loadMoreInfo)
                    }
                }, onFailure = { errorMsg ->
            view.loadFailure(errorMsg)
        })
    }

    override fun openActivity(info: ChannelInfo) {
        when (info.needOrder) {
            0 -> view.openTransferActivity(info.id.toString())
            1 -> view.openUploadPicTokenActivity(info.id.toString())
        }
    }
}